package com.wonders.hisdoctor.station.project.laboratory.controller;

import com.wonders.his.common.bean.BaseResult;
import com.wonders.his.common.exception.CustomException;
import com.wonders.his.common.utils.StringUtils;
import com.wonders.his.framework.dto.LoginUserDTO;
import com.wonders.his.framework.service.TokenService;
import com.wonders.hisdoctor.station.enums.ChargeIndicatorEnum;
import com.wonders.hisdoctor.station.project.laboratory.service.ILaboratoryService;
import com.wonders.hisdoctor.station.project.laboratory.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * Description: 检验申请
 *
 * @author zhaogongming
 * <p>
 * Create: 2021-02-04
 */
@Api(tags = "检验申请")
@RestController
@Validated
@RequestMapping("/laboratory")
public class LaboratoryController {

    @Autowired
    private ILaboratoryService laboratoryService;

    @Autowired
    private TokenService tokenService;

    @ApiOperation("获取检验科室列表")
    @GetMapping("/selectDeptList")
    public BaseResult<List<DeptResponseVo>> selectDeptList(@RequestParam("hospitalCode") String hospitalCode) {
        return BaseResult.success(laboratoryService.selectDeptList(hospitalCode));
    }

    @ApiOperation("联动获取检验项目")
    @PostMapping("/selectLaboratoryList")
    public BaseResult<List<LaboratoryResponseVo>> selectLaboratoryList(@RequestBody @Valid LaboratoryRequestVo laboratoryRequestVo) {
        return BaseResult.success(laboratoryService.selectLaboratoryList(laboratoryRequestVo));
    }

    @ApiOperation("提交申请")
    @PostMapping("/save")
    public BaseResult<Integer> save(@RequestBody @Valid LaboratorySaveRequestVo laboratorySaveVo) {
        return BaseResult.success(laboratoryService.saveNew(laboratorySaveVo));
    }

    @ApiOperation("已申请检验单列表")
    @PostMapping("/selectLaboratory")
    public BaseResult<List<LaboratoryResponseVo>> selectLaboratory(@RequestBody @Valid LaboratoryInfoRequestVo laboratoryInfoRequestVo) {
        return BaseResult.success(laboratoryService.selectLaboratoryNew(laboratoryInfoRequestVo));
    }

    @ApiOperation("历史申请")
    @PostMapping("/selectHistoryLaboratory")
    public BaseResult<List<LaboratoryResponseVo>> selectHistoryLaboratory(@RequestBody LaboratoryHistoryRequestVo laboratoryHistoryRequestVo, HttpServletRequest request) {
        LoginUserDTO loginUserDTO = tokenService.getLoginUserDTO(tokenService.getToken(request));
        if (loginUserDTO != null && loginUserDTO.getSysSysuserVo() != null) {
            laboratoryHistoryRequestVo.setDoctor(loginUserDTO.getSysSysuserVo().getEmpNo());
        }
        //如果是查询已开单的历史申请，当前医生工号必传，为了排除当前登录医生的已开单申请。--历史申请不展示当前登录医生的已开单申请
        if (null != laboratoryHistoryRequestVo.getChargeIndicator()) {
            if (ChargeIndicatorEnum.ORDERED.getCode() == laboratoryHistoryRequestVo.getChargeIndicator() && StringUtils.isEmpty(laboratoryHistoryRequestVo.getDoctor())) {
                throw new CustomException("当前登录医生工号不能为空");
            }
        }
        return BaseResult.success(laboratoryService.selectHistoryLaboratory(laboratoryHistoryRequestVo));
    }

    @ApiOperation("删除申请")
    @PostMapping("/cancelLaboratory")
    public BaseResult<Integer> cancelLaboratory(@RequestBody @Valid List<CancelLaboratoryRequestVo> list) {
        return BaseResult.success(laboratoryService.cancelLaboratory(list));
    }

    @ApiOperation("打印申请单")
    @PostMapping("/selectPrintInfo")
    public BaseResult<List<LaboratoryPrintReponseVo>> selectPrintInfo(@RequestBody @Valid List<LaboratoryPrintRequestVo> list) {
        return BaseResult.success(laboratoryService.selectPrintInfo(list));
    }

    @ApiOperation("保存并打印")
    @PostMapping("/saveAndPrint")
    public BaseResult<List<LaboratoryPrintReponseVo>> saveAndPrint(@RequestBody @Valid LaboratorySaveRequestVo laboratorySaveVo){
        return BaseResult.success(laboratoryService.saveAndPrintNew(laboratorySaveVo));
    }

}
